<?php 
class AttTutUtils {
  public static function student_period($myPdo, $student_id, $start, $end) {
    $cond_vals = new stdClass();
    $cond_vals->c = 't.attr2 = :v1 AND t1.student_id = :v2';
    $cond_vals->v = array(':v1' => 0, ':v2' => $student_id);
    $opts = new stdClass();
    $opts->select_expr = 't.date1, t.date2, t.id AS semester_id, t1.id AS enrolled_semester_id';
    $opts->join_expr = "LEFT JOIN " . TABLE_NAME_PREFIX_PLUGIN . "student_enrolled_semesters as t1 ON t1.semester_id = t.id";
    $semesters = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'semester_semesters', $cond_vals, NULL, NULL, NULL, NULL, $opts)->d;

    //semesters
    return AttTutUtils::_semesters_atdn($semesters,$start,$end,$myPdo);
  }
  
  public static function student_class_period($myPdo, $student_id, $class_id) {
    $cond_vals = new stdClass();
    $cond_vals->c = 't.student_id = :v1 AND t.class_id = :v2';
    $cond_vals->v = array(':v1' => $student_id, ':v2' => $class_id);
    $enrolled_class = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'student_enrolled_classes', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d[0];
    if($enrolled_class -> status_id == STATUS_CLEAN_RECORDS){
      return $attendence = -1;
    }
    
    $cond_vals->c = 't1.id = :v1';
    $cond_vals->v = array(':v1' => $class_id);
    $opts = new stdClass();
    $opts->select_expr = 't.date1, t.date2, t.id AS semester_id, t1.attr5';
    $opts->join_expr = "LEFT JOIN " . TABLE_NAME_PREFIX_PLUGIN . "semester_classes as t1 ON t1.attr5 = t.id";
    $semester = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'semester_semesters', $cond_vals, NULL, NULL, NULL, NULL, $opts)->d[0];
    //obj {semester_id, date1, date2}
    $s = strtotime($semester->date1); 
    $lastsunday = time($semester);
    if (date('W', time()) != 0) {
      $lastsunday = strtotime(date('Y-m-d',strtotime('last Sunday', time())));
    }
    $e = strtotime(date("Y-m-d", min($lastsunday, strtotime($semester->date2))));
    
    if($enrolled_class -> status_id == STATUS_KEEP_RECORDS && strtotime($enrolled_class -> wd_date) < $e){
      $e = strtotime($enrolled_class -> wd_date);
    }
    //s, e
    $weeks = AttTutUtils::_weeks($s, $e);
    //weeks
    $cond_vals->c = 't.semester_id = :v1';
    $cond_vals->v = array(':v1' => $semester->attr5);
    $no_tutorial_weeks = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'semester_no_tutorial_weeks', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
    //no tutorial weeks
    $final_weeks = AttTutUtils::_tutorial_weeks($weeks, $no_tutorial_weeks);
    //final weeks
    $total = count($final_weeks);
    $signs = 0;
    for($i=0; $i<count($final_weeks); $i++){
      if($enrolled_class -> status_id == STATUS_KEEP_RECORDS){
        $cond_vals->c = '(t.date >= :v1 AND t.date <= :v2 AND t.date < :v5) AND t.is_tutorial = 1 AND t.class_id = :v3 AND t.student_id = :v4';
        $cond_vals->v = array(':v1' => $final_weeks[$i][0], ':v2' => $final_weeks[$i][1], ':v3' => $class_id, ':v4' => $student_id, ':v5' => $enrolled_class -> wd_date);
      } else {
        $cond_vals->c = '(t.date >= :v1 AND t.date <= :v2) AND t.is_tutorial = 1 AND t.class_id = :v3 AND t.student_id = :v4';
        $cond_vals->v = array(':v1' => $final_weeks[$i][0], ':v2' => $final_weeks[$i][1], ':v3' => $class_id, ':v4' => $student_id);
      }
      $opts = new stdClass();
      $opts->select_expr = 'COUNT(t.id) AS count';
      $signs += DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'attendences_absents', $cond_vals, NULL, NULL, NULL, NULL, $opts)->d[0]->count;
    }
    if($total == 0){
      return $attendence = -1;
    }
    
    

    return $attendence = round($signs/$total,4);
  }
  
  public static function enrolled_program_period($myPdo, $enrolled_program_id, $start, $end) {
    $cond_vals = new stdClass();
    $cond_vals->c = 't.attr2 = :v1 AND t1.enrolled_program_id = :v2'; 
    $cond_vals->v = array(':v1' => 0, ':v2' => $enrolled_program_id);
    $opts = new stdClass();
    $opts->select_expr = 't.date1, t.date2, t.id AS semester_id, t1.id AS enrolled_semester_id';
    $opts->join_expr = "LEFT JOIN " . TABLE_NAME_PREFIX_PLUGIN . "student_enrolled_semesters as t1 ON t1.semester_id = t.id";
    $semesters = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'semester_semesters', $cond_vals, NULL, NULL, NULL, NULL, $opts)->d;

    //semesters{date1, date2, semester_id, enrolled_semester_id, _papers}
    return AttTutUtils::_semesters_atdn($semesters,$start,$end,$myPdo);
  }
  
  public static function enrolled_semester_period($myPdo, $enrolled_semester_id, $start, $end) {
    $cond_vals = new stdClass();
    $cond_vals->c = 't1.id = :v1';
    $cond_vals->v = array(':v1' => $enrolled_semester_id);
    $opts = new stdClass();
    $opts->select_expr = 't.date1, t.date2, t.id AS semester_id, t1.id AS enrolled_semester_id';
    $opts->join_expr = "LEFT JOIN " . TABLE_NAME_PREFIX_PLUGIN . "student_enrolled_semesters as t1 ON t1.semester_id = t.id";
    $semester = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'semester_semesters', $cond_vals, NULL, NULL, NULL, NULL, $opts)->d[0];
    //

    //
    return AttTutUtils::_semesters_atdn(array($semester),$start,$end,$myPdo);
  }
  
  public static function _semesters_atdn($semesters, $start, $end, $myPdo) {
    $total = 0;
    $signs = 0;
    $s1 = strtotime($start);
    if (date('W', strtotime($start)) != 1) {
      $s1 = strtotime(date('Y-m-d',strtotime('last Monday', strtotime($start))));
    }
    $e1 = strtotime($end);
    if (date('W', strtotime($end)) != 0) {
      $e1 = strtotime(date('Y-m-d',strtotime('last Sunday', strtotime($end))));
    }
    $e2 = time();
    if (date('W', time()) != 0) {
      $e2 = strtotime(date('Y-m-d',strtotime('last Sunday', time())));
    }
    for($i=0; $i<count($semesters); $i++){
      $cond_vals->c = 't.enrolled_semester_id = :v1';
      $cond_vals->v = array(':v1' => $semesters[$i]->enrolled_semester_id);
      $enrolled_classes = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'student_enrolled_classes', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
    	//enrolled_class
      $s = max(strtotime($semesters[$i]->date1),$s1);
      $e = min(strtotime($semesters[$i]->date2),$e1,$e2);
      $weeks = AttTutUtils::_weeks($s, $e);
      //week{[0, 1]}

      $cond_vals->c = 't.semester_id = :v1';
      $cond_vals->v = array(':v1' => $semesters[$i]->semester_id);
      $no_tutorial_weeks = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'semester_no_tutorial_weeks', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
      //week{[0, 1]}
      $final_weeks = AttTutUtils::_tutorial_weeks($weeks, $no_tutorial_weeks);
      //week{[0, 1]}
      
      for($j=0; $j<count($final_weeks); $j++) {
        $cond_vals->c = '(t.date >= :v1 AND t.date <= :v2) AND t.is_tutorial = 1 AND t.enrolled_semester_id = :v3';
        $cond_vals->v = array(':v1' => $final_weeks[$j][0], ':v2' => $final_weeks[$j][1], ':v3' => $semesters[$i]->enrolled_semester_id);
        $tmp = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'attendences_absents', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
        $signs += AttTutUtils::_get_signs($tmp,$enrolled_classes);
        for($n=0; $n<count($enrolled_classes); $n++){
          if($enrolled_classes[$n]->status_id == STATUS_KEEP_RECORDS){
            if(strtotime($enrolled_classes[$n]->wd_date) < $e){
              $total += 0;
            } else {
              $total += 1;
            }
          } else if($enrolled_classes[$n]->status_id == STATUS_CLEAN_RECORDS){
             $total += 0;
          } else {
            $total += 1;
          }
        }
        //foreach enrolled_class
        //check status, wd_date if status = withdrawn - keep records
        //if ok, total+ $total += 1 AND signs += _get_signs(all_signs)
        //otherwise, skip
      }
    }
    if($total <= 0){
      return $attendence = -1;
    }
    return $attendence = round($signs/$total,4);
  }
  
  public static function _get_signs($tmp, $classes) {
    $signs = count($tmp);
    for($j=0; $j<count($tmp); $j++){
      for($i=0; $i<count($classes); $i++){
        if($tmp[$j]->class_id == $classes[$i]->class_id && (($classes[$i]->status_id == STATUS_KEEP_RECORDS && strtotime($tmp[$j]->date) > strtotime($classes[$i]->wd_date)) || $classes[$i]->status_id == STATUS_CLEAN_RECORDS)){
        	$signs -= 1;
        }
      }
    }
    return $signs;
  }
  
  public static function _weeks($start, $end) {
    $weeks = array();
    for($j=0; $j<($end - $start + 86400)/(86400*7); $j++){
      array_push($weeks,array(date('Y-m-d',$start+$j*86400*7),date('Y-m-d',$start+86400*6+$j*86400*7)));
    }
    return $weeks;
  }
  
  public static function _tutorial_weeks($weeks, $no_weeks) {
    $final_weeks = array();
    if(count($no_weeks) == 0){
      return $weeks;
    }
    for($n=0; $n<count($weeks); $n++){
      $exist = false;
      for($m=0; $m<count($no_weeks); $m++){
        if(strtotime($no_weeks[$m]->date1) == strtotime($weeks[$n][0])){
          $exist = true;
        }
      }
      if (!$exist) { 
        $final_weeks[] = $weeks[$n];
      }
    }
    return $final_weeks;
  }
}
?>
